#include <iostream>
#include <vector>
#include <cmath>

struct Node {
    Node* left;
    Node* right;
    int data;

    Node(int data) 
        : left(NULL), right(NULL), data(data) {}
};

int summation(int n) 
{
    int sum;

    for (int i = 0; i <= n; i++) {
        sum += exp2(i);
    }

    return sum;
}

int findMidNumIdx(std::vector<int>& array)
{
    int size = array.size();

    if (size & 0x1) {
        return size / 2;
    } else {
        return size / 2 - 1;
    }
}

void insertNode(Node*& root, int n)
{
    Node* newNode = new Node(n);

    if (root == NULL) {
        root = newNode;
        return ;
    }

    Node* currentNode = root;
    while (1) {
        if (n <= currentNode->data) {
            if (currentNode->left == NULL) {
                currentNode->left = newNode;
                return ;
            } else {
                currentNode = currentNode->left;
            }
        } else {
            if (currentNode->right == NULL) {
                currentNode->right = newNode;
                return ;
            } else {
                currentNode = currentNode->right;
            }
        }
    }
}

int layerCounter(Node* root)
{
    if (root == NULL)
        return 0;
    return std::max(layerCounter(root->left), layerCounter(root->right)) + 1;
}

void printTree(Node* root) 
{
    if (root == NULL)
        return ;

    std::cout << root->data << "(" << layerCounter(root) << ") ";
    printTree(root->left);
    printTree(root->right);
}

int main(void) 
{
    std::cout << "Please enter a series of number: (enter -1 to abort input)" << std::endl;

    int n;
    std::vector<int> array;
    while (std::cin >> n) {
        if (n == -1)
            break;
        array.push_back(n);
    }

    if (array.empty()) {
        std::cout << "No numbers entered!" << std::endl;
        return 1;
    }

    Node* root = NULL;
    int midIdx = findMidNumIdx(array);
    insertNode(root, array[midIdx]);

    //LeftTree
    for (int i = midIdx - 2; i >= 0; i -= 2) {
        insertNode(root, array[i]);
    }

    //RightTree
    for (int i = midIdx + 2; i < array.size(); i += 2) {
        insertNode(root, array[i]);
    }

    //CompleteLeftTreeRightNode
    for (int i = midIdx - 1; i >= 0; i -= 2) {
        insertNode(root, array[i]);
    }
    //CompleteRightTreeLeftNode
    for (int i = midIdx + 1; i < array.size(); i += 2) {
        insertNode(root, array[i]);
    }

    printTree(root);
    std::cout << std::endl;
    n = layerCounter(root);
    std::cout << "TotalLayers:" << n << std::endl; 

    return 0;
}